package com.vhall.component.controller.upload;


import com.alibaba.fastjson.JSONObject;
import com.vhall.component.entity.upload.vo.ConsoleUploadVo;
import com.vhall.component.entity.upload.vo.DocUploadVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.framework.common.exception.GlobalErrorCode;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.utils.DirUtil;
import com.vhall.component.plugin.common.utils.UploadUtil;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.service.upload.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

/**
 * @author yongzheng.xu
 * @date 2021/5/26 17:59
 */
@Slf4j
@VhallComponent
public class UploadController implements UploadFacade {


    @Autowired
    private UploadService uploadService;


    @Autowired
    private PaasService paasService;

    /**
     * console端-聊天图片上传
     *
     * @param vo 参数
     * @return 上传后返回的url路径
     */
    @Override
    public String imageUpload(ConsoleUploadVo vo) {
        return imageUploadMain(vo);
    }

    /**
     * admin端图片上传
     *
     * @param vo 参数
     * @return 上传后返回的url路径
     */
    @Override
    @TokenAuth("sys:upload:image")
    public String imageUploadAdmin(ConsoleUploadVo vo) {
        return imageUploadMain(vo);
    }

    private String imageUploadMain(ConsoleUploadVo vo) {
        if (vo.getFile() == null) {
            throw new BusinessException(BizErrorCode.TYPE_INVALID_IMAGE);
        }
        String[] types = {"jpeg", "jpg", "gif", "png", "bmp"};
        String imageType = StringUtils.substringAfterLast(vo.getFile().getOriginalFilename(), ".");
        if (Boolean.FALSE.equals(ArrayUtils.contains(types, imageType))) {
            throw new BusinessException(BizErrorCode.TYPE_INVALID_IMAGE);
        }
        DirUtil.failIfDirectoryTraversal(vo.getFile().getOriginalFilename());

        Map<String, String> result = uploadService.doUpload(vo.getFile(), "");

        if (null == result || !result.containsKey("url")) {
            throw new BusinessException(GlobalErrorCode.FAILED);
        }
        return result.get("url");
    }


    @Override
    public Object docUpload(DocUploadVO vo) {
        try {
            MultipartFile document = vo.getDocument();
            String name = document.getOriginalFilename();

            // 获取真实路径，对应${项目目录}/uploads，当然，这个目录必须存在
            String path = System.getProperty("java.io.tmpdir");

            File dest = UploadUtil.upload(document, path, "");

            //PaaS端创建文档信息
            JSONObject result = paasService.documentCreate(dest);
            if (null == result) {
                throw new BusinessException(BizErrorCode.BIZ_UPLOAD_FAILED);
            }
            return result;
        } catch (Exception ex) {
            log.error("文档上传失败:", ex);
        }

        return null;
    }
}
